using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Emlyn.Website.Articles;
using System.ComponentModel;

namespace Emlyn.Website.ArticlesPresentation
{
    public enum RelationshipType { ChildArticles, ParentArticles };

    public class PresentArticleRelatedArticles: PresentArticle
    {
        //protected TextBox _edArticleListName;
        //protected TextBox _edArticleListCode;
        //protected TextBox _edArticleListDescription;
        protected DropDownList _dropDownListAdd;

        public void OnAdd(Object o, System.EventArgs ea)
        {
            string larticleCodeAdd = _dropDownListAdd.SelectedValue;
            if (!String.IsNullOrEmpty(larticleCodeAdd))
            {
                ArticleQuery laq = new ArticleQuery();
                ArticleMod lam = new ArticleMod();
                Article larticleToAdd = laq.GetArticleByArticleCode(larticleCodeAdd);
                if (aArticleType == RelationshipType.ChildArticles)
                    lam.AddArticleRelationship(aArticle.ArticleID, larticleToAdd.ArticleID);
                else
                    lam.AddArticleRelationship(larticleToAdd.ArticleID, aArticle.ArticleID);
            }
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        public void OnDelete(Object o, System.EventArgs ea)
        {
            string lArticleCodeDel = (o as LinkButton).ID.Split('_')[1];

            if (!String.IsNullOrEmpty(lArticleCodeDel))
            {
                ArticleQuery laq = new ArticleQuery();
                ArticleMod lam = new ArticleMod();
                Article larticleToDel = laq.GetArticleByArticleCode(lArticleCodeDel);
                if (aArticleType == RelationshipType.ChildArticles)
                    lam.RemoveArticleRelationship(aArticle.ArticleID, larticleToDel.ArticleID);
                else
                    lam.RemoveArticleRelationship(larticleToDel.ArticleID, aArticle.ArticleID);
            }
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        public void OnMoveUp(Object o, System.EventArgs ea)
        {
            string lArticleCodeMoveUp = (o as LinkButton).ID.Split('_')[1];

            if (!String.IsNullOrEmpty(lArticleCodeMoveUp))
            {
                ArticleQuery laq = new ArticleQuery();
                ArticleMod lam = new ArticleMod();
                Article larticleToMoveUp = laq.GetArticleByArticleCode(lArticleCodeMoveUp);

                lam.MoveArticleRelationshipUp(aArticle.ArticleID, larticleToMoveUp.ArticleID);
            }
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        public void OnMoveDown(Object o, System.EventArgs ea)
        {
            string lArticleCodeMoveDown = (o as LinkButton).ID.Split('_')[1];

            if (!String.IsNullOrEmpty(lArticleCodeMoveDown))
            {
                ArticleQuery laq = new ArticleQuery();
                ArticleMod lam = new ArticleMod();
                Article larticleToMoveDown = laq.GetArticleByArticleCode(lArticleCodeMoveDown);

                lam.MoveArticleRelationshipDown(aArticle.ArticleID, larticleToMoveDown.ArticleID);
            }
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        private string GenerateCode()
        {
            string lcode = ((new Random()).Next(99999999)).ToString();
            ArticleQuery laq = new ArticleQuery();

            while (laq.GetArticleByArticleCode(lcode) != null)
            {
                lcode = ((new Random()).Next(99999999)).ToString();
            };

            return lcode;
        }

        public void OnNew(Object o, System.EventArgs ea)
        {
            string lArticleCode = GenerateCode();

            Article larticleNew = new Article();
            larticleNew.ArticleCode = lArticleCode;
            larticleNew.ArticleName = "New Article " + larticleNew.ArticleCode;
            larticleNew.ArticleContent = "Description: " + larticleNew.ArticleName;

            ArticleMod lam = new ArticleMod();
            lam.Save(larticleNew);

            if (aArticleType == RelationshipType.ChildArticles)
                lam.AddArticleRelationship(aArticle.ArticleID, larticleNew.ArticleID);
            else
                lam.AddArticleRelationship(larticleNew.ArticleID, aArticle.ArticleID);

            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        public void OnOrderByDate(Object o, System.EventArgs ea)
        {
            (new ArticleMod()).ReorderChildrenByLastDate(aArticle.ArticleID);

            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        public void OnOrderByName(Object o, System.EventArgs ea)
        {
            (new ArticleMod()).ReorderChildrenByName(aArticle.ArticleID);

            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        private IList<Article> GetChildArticles()
        {
            ArticleQuery laq = new ArticleQuery();

            return laq.GetArticlesForParentArticleID(aArticle.ArticleID);
        }

        private IList<Article> GetNonChildArticles()
        {
            ArticleQuery laq = new ArticleQuery();

            return laq.GetArticlesNotForParentArticleID(aArticle.ArticleID);
        }

        private IList<Article> GetParentArticles()
        {
            ArticleQuery laq = new ArticleQuery();

            return laq.GetArticlesForChildArticleID(aArticle.ArticleID);
        }

        private IList<Article> GetNonParentArticles()
        {
            ArticleQuery laq = new ArticleQuery();

            return laq.GetArticlesNotForChildArticleID(aArticle.ArticleID);
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Title
        {
            get
            {
                return Convert.ToString(ViewState["Title"]);
            }
            set
            {
                ViewState["Title"] = Convert.ToString(value);
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public RelationshipType aArticleType
        {
            get
            {
                if (ViewState["ArticleType"] == null)
                    return RelationshipType.ChildArticles;
                else
                    return (RelationshipType)Enum.Parse(
                            typeof(RelationshipType), 
                            Convert.ToString(ViewState["ArticleType"])
                        );
            }
            set
            {
                ViewState["ArticleType"] = Convert.ToString(value);
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string NavigateUrl
        {
            get
            {
                return Convert.ToString(ViewState["NavigateUrl"]);
            }
            set
            {
                ViewState["NavigateUrl"] = Convert.ToString(value);
            }
        }



        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Panel lbasePanel = new Panel();
            lbasePanel.CssClass = CssClass + "RelatedArticles";
            Controls.Add(lbasePanel);

            Literal llit;
            Panel lpnl;
            Panel lsubpnl;

            lpnl = new Panel();
            lpnl.CssClass = lbasePanel.CssClass + "Title";
            lbasePanel.Controls.Add(lpnl);

            llit = new Literal();
            llit.Text = Title;
            lpnl.Controls.Add(llit);

            lbasePanel.Controls.Add(lpnl);

            lpnl = new Panel();
            lpnl.CssClass = CssClass + "Articles";

            IList<Article> lArticles;
            if (aArticleType == RelationshipType.ChildArticles)
                lArticles = GetChildArticles();
            else
                lArticles = GetParentArticles();

            int lIDPrefix = 0;
            int lpos = 0;
            foreach (Article larticle in lArticles)
            {
                lsubpnl = new Panel();
                lsubpnl.CssClass = lpnl.CssClass + "Article";

                HyperLink lhref = new HyperLink();
                lhref.NavigateUrl = NavigateUrl.Replace("%ArticleCode%", larticle.ArticleCode);
                lhref.Text = larticle.ArticleName;
                lsubpnl.Controls.Add(lhref);

                if (Editable)
                {
                    Label llbl = new Label();
                    llbl.Text = " ";
                    lsubpnl.Controls.Add(llbl);

                    LinkButton ldeleteLink = new LinkButton();
                    Image ldeleteImage = new Image();
                    ldeleteImage.ImageUrl = "images/delete.png";
                    ldeleteImage.Width = 20;
                    ldeleteImage.Height = 20;
                    ldeleteImage.AlternateText = "delete";
                    ldeleteLink.Controls.Add(ldeleteImage);
                    ldeleteLink.ID = Convert.ToString(lIDPrefix++) + "_" + larticle.ArticleCode;
                    ldeleteLink.Click += OnDelete;
                    ldeleteLink.CssClass = lsubpnl.CssClass + "DeleteButton";
                    ldeleteLink.OnClientClick = "return confirm('Remove this article relationship?');";
                    lsubpnl.Controls.Add(ldeleteLink);

                    if (aArticleType == RelationshipType.ChildArticles)
                    {
                        llbl = new Label();
                        llbl.Text = " ";
                        lsubpnl.Controls.Add(llbl);

                        LinkButton lmoveUpLink = new LinkButton();
                        Image lmoveUpImage = new Image();
                        lmoveUpImage.ImageUrl = "images/moveup.png";
                        lmoveUpImage.Width = 20;
                        lmoveUpImage.Height = 20;
                        lmoveUpImage.AlternateText = "up";
                        lmoveUpLink.Controls.Add(lmoveUpImage);
                        lmoveUpLink.ID = Convert.ToString(lIDPrefix++) + "_" + larticle.ArticleCode;
                        lmoveUpLink.Click += OnMoveUp;
                        lmoveUpLink.CssClass = lsubpnl.CssClass + "MoveUpButton";
                        lmoveUpLink.Enabled = (lpos > 0);
                        lsubpnl.Controls.Add(lmoveUpLink);

                        llbl = new Label();
                        llbl.Text = " ";
                        lsubpnl.Controls.Add(llbl);

                        LinkButton lmoveDownLink = new LinkButton();
                        Image lmoveDownImage = new Image();
                        lmoveDownImage.ImageUrl = "images/movedown.png";
                        lmoveDownImage.Width = 20;
                        lmoveDownImage.Height = 20;
                        lmoveDownImage.AlternateText = "down";
                        lmoveDownLink.Controls.Add(lmoveDownImage);
                        lmoveDownLink.ID = Convert.ToString(lIDPrefix++) + "_" + larticle.ArticleCode;
                        lmoveDownLink.Click += OnMoveDown;
                        lmoveDownLink.CssClass = lsubpnl.CssClass + "MoveDownButton";
                        lmoveDownLink.Enabled = (lpos < (lArticles.Count - 1));
                        lsubpnl.Controls.Add(lmoveDownLink);
                    }
                }
                lpnl.Controls.Add(lsubpnl);

                lpos++;
            }

            lbasePanel.Controls.Add(lpnl);

            if (Editable)
            {
                lpnl = new Panel();
                lpnl.CssClass = CssClass + "Add";

                _dropDownListAdd = new DropDownList();
                _dropDownListAdd.CssClass = lpnl.CssClass + "DropDownList";
                ListItem lli = new ListItem("(Select)", String.Empty);
                _dropDownListAdd.Items.Add(lli);

                if (aArticleType == RelationshipType.ChildArticles)
                    lArticles = GetNonChildArticles();
                else
                    lArticles = GetNonParentArticles();
                foreach (Article larticle in lArticles)
                {
                    lli = new ListItem(larticle.ArticleName.Substring(0, Math.Min(larticle.ArticleName.Length, 30)), larticle.ArticleCode);
                    _dropDownListAdd.Items.Add(lli);
                }

                lpnl.Controls.Add(_dropDownListAdd);

                Button lbutton = new Button();
                lbutton.Text = "Add";
                lbutton.Click += new EventHandler(OnAdd);
                lbutton.CssClass = lpnl.CssClass + "Button";

                lpnl.Controls.Add(lbutton);

                lsubpnl = new Panel();
                lsubpnl.CssClass = lpnl.CssClass + "New";
                lpnl.Controls.Add(lsubpnl);

                lbutton = new Button();
                lbutton.Text = "New";
                lbutton.Click += new EventHandler(OnNew);
                lbutton.CssClass = lsubpnl.CssClass + "Button";

                lsubpnl.Controls.Add(lbutton);

                if (aArticleType == RelationshipType.ChildArticles)
                {
                    lsubpnl = new Panel();
                    lsubpnl.CssClass = lpnl.CssClass + "OrderByDate";
                    lpnl.Controls.Add(lsubpnl);

                    lbutton = new Button();
                    lbutton.Text = "Order By Date";
                    lbutton.Click += new EventHandler(OnOrderByDate);
                    lbutton.CssClass = lsubpnl.CssClass + "Button";

                    lsubpnl.Controls.Add(lbutton);

                    lsubpnl = new Panel();
                    lsubpnl.CssClass = lpnl.CssClass + "OrderByName";
                    lpnl.Controls.Add(lsubpnl);

                    lbutton = new Button();
                    lbutton.Text = "Order By Name";
                    lbutton.Click += new EventHandler(OnOrderByName);
                    lbutton.CssClass = lsubpnl.CssClass + "Button";

                    lsubpnl.Controls.Add(lbutton);
               }

                lbasePanel.Controls.Add(lpnl);
            }
        }
    }
}
